/*
 *
 *  * AUTHOR：SIGHT
 *  *
 *  * DESCRIPTION：create the File, and add the content.
 *  *
 *  * Copyright © ZhiMore. All Rights Reserved
 *  *
 *
 */

package com.lx.commonlib.utils;

import android.content.Context;
import android.os.CountDownTimer;
import android.os.Handler;
import android.support.annotation.DrawableRes;
import android.support.annotation.StringRes;
import android.view.LayoutInflater;
import android.view.View;
import android.widget.TextView;
import android.widget.Toast;

import com.lx.commonlib.R;

public class ToastUtil {

    public static final String TAG="----"+ToastUtil.class.getSimpleName();

    //显示当前的toast需要的时间
    private static long needTime=0;

    //最后一个toast发送的时间
    private static long lastTime=0;

    //背景图片的id
    private static int bgResId=R.drawable.shape_toast_bg;

    private static Toast toast;

    private static Context mContext;

    /**
     * 修改原布局的Toast
     */
    private ToastUtil() {

    }

    public static void post(Runnable r,int type){

        /** 计算toast延时的时间*/
        //获取当前的时间
        long currentTime=System.currentTimeMillis();

        long dTime=currentTime-lastTime;

        if(dTime>=needTime){ //如果时间间隔大于等于前面的toast需要的时间,重置为0
            needTime=0;
            LogUtil.d(TAG,"没有toast正在执行");
        }else{//如果时间间隔小于于前面的toast需要的时间,则减去这段时间间隔
            needTime-=dTime;
            LogUtil.d(TAG,"有toast正在执行");
        }

        lastTime=currentTime;

        //使用工具类在主线程执行
        UIHandlerUtil.getInstance().postDelayed(r,needTime);

        if(type==Toast.LENGTH_SHORT){ //短时间
            needTime+=2000;
            LogUtil.d(TAG,"needTime+2000="+needTime);
        }else{
            needTime+=3500;
            LogUtil.d(TAG,"needTime+3500="+needTime);
        }
    }

    /**
     * 方法说明:  普通的toast样式,短时间显示
     * @param
     *@author liuxiong
     *@time 2017/7/20 15:22
     */
    public static void show(final String message){
        post(new Runnable() {
            @Override
            public void run() {
                makeToast(Toast.LENGTH_SHORT,message,bgResId).show();
            }
        },Toast.LENGTH_SHORT);
    }
    /**
     * 方法说明:  普通的toast样式,短时间显示, 可以设置背景图片
     * @param drawId 背景图片的资源ID
     *@author liuxiong
     *@time 2017/7/20 15:22
     */
    public static void show(final String message,@DrawableRes final int drawId){
        post(new Runnable() {
            @Override
            public void run() {
                makeToast(Toast.LENGTH_SHORT,message,drawId).show();
            }
        },Toast.LENGTH_SHORT);
    }

    /**
     * 方法说明: 普通的toast样式,短时间显示
     * @param  resId 要toast的信息的资源id
     *@author liuxiong
     *@time 2017/7/28 17:00
     */
    public static void show(@StringRes final int resId){

        post(new Runnable() {
            @Override
            public void run() {
                makeToast(Toast.LENGTH_SHORT,mContext.getString(resId),bgResId).show();
            }
        },Toast.LENGTH_SHORT);
    }


    /**
     * 方法说明:  普通的toast样式,短时间显示
     * @param
     *@author liuxiong
     *@time 2017/7/20 15:22
     */
    public static void showLong(final String message){
        post(new Runnable() {
            @Override
            public void run() {
                makeToast(Toast.LENGTH_LONG,message,bgResId).show();
            }
        },Toast.LENGTH_LONG);

    }

    /**
     * 方法说明:  创建一个toast
     * @param
     *@author liuxiong
     *@time 2017/7/20 15:28
     */
    private static Toast makeToast(int duration, String message,@DrawableRes int drawId) {
        //创建对象
        toast = Toast.makeText(mContext,message,duration);
        //创建自定义布局
        LayoutInflater inflate = (LayoutInflater)
                mContext.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
        View v = inflate.inflate(R.layout.view_toast, null);

        //设置背景图片
        if(drawId!=0){
            //v.setBackground(ResourcesUtil.getDrawable(drawId));
            v.setBackgroundResource(drawId);
        }

        //设置自定义布局到Toast
        toast.setView(v);

        //设置文字
        TextView tv = v.findViewById(R.id.tv_message);
        tv.setText(message);

        toast.setDuration(duration);
        return toast;
    }

}
